package com.example.myprojects.fragment;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.myprojects.MainActivity;
import com.example.myprojects.MyListApapter;
import com.example.myprojects.MyNews;
import com.example.myprojects.NewsActivity;
import com.example.myprojects.R;
import com.example.myprojects.service.MyService;
import com.example.myprojects.uitl.HttpUtil;
import com.example.myprojects.uitl.MyListBean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {
    List<MyNews.ResultBean.DataBean>list;
    ListView listView;
    MyNews myNews;
    MyFargReceiver myFargReceiver;
    MyListApapter apapter;
    String url;
    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_home, container, false);
        listView=view.findViewById(R.id.listView);
        //TextView tvvv=view.findViewById(R.id.tvvv);

         Intent intent=new Intent(getActivity(), MyService.class);
        getActivity().startService(intent);

        myFargReceiver=new MyFargReceiver();
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("com.example.myprojects");
        MyFargReceiver myReceiver=new MyFargReceiver();
        list=new ArrayList<>();
        getActivity().registerReceiver(myReceiver,intentFilter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                url= list.get(position).getUrl();
                Intent intent1=new Intent(getActivity(), NewsActivity.class);
                intent1.putExtra("url",url);
                startActivity(intent1);
            }
        });
        return view;
    }
   public  class MyFargReceiver extends BroadcastReceiver{
       @Override
       public void onReceive(Context context, Intent intent) {
           if ("com.example.myprojects".equals(intent.getAction())){
               myNews= (MyNews) intent.getSerializableExtra("mynews");
               list=myNews.getResult().getData();
               for (int i=0;i<list.size();i++){
                   MyNews.ResultBean.DataBean dataBean=list.get(i);

                   if (dataBean.getThumbnail_pic_s03()==null){
                       dataBean.setType(0);
                   }else{
                       dataBean.setType(1);
                   }

                   apapter=new MyListApapter(list,getActivity());
                   listView.setAdapter(apapter);
               }
           }
       }

   }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(myFargReceiver);
    }
}
